package org.example.service.impl;

import cn.hutool.core.lang.Snowflake;
import org.example.domain.UtcItemInfo;
import org.example.domain.UtcItemRecord;
import org.example.dto.PublishContentDto;
import org.example.dto.UpdateContentDto;
import org.example.mapper.ContentPublishAndAuditMapper;
import org.example.mapper.UtcItemInfoMapper;
import org.example.mapper.UtcItemRecordMapper;
import org.example.service.ContentPublishAndAuditService;
import org.example.userToContentEnum.AuditEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @FileName ContentPublishAndAuditImpl
 * @Description
 * @Author coderfun
 * @selfDesc coding in fun
 * @date 2024-11-27
 **/
@Service
public class ContentPublishAndAuditImpl implements ContentPublishAndAuditService {
    @Autowired
    private ContentPublishAndAuditMapper contentPublishAndAuditMapper;
    @Autowired
    private UtcItemInfoMapper utcItemInfoMapper;
    @Autowired
    private UtcItemRecordMapper utcItemRecordMapper;

    @Value("")
    private Integer maxLimitFunNum;
    private static Snowflake snowflake = new Snowflake(1, 1);
    //默认版本号
    private static final Long defaultVersion = 1l;

    @Override
    @Transactional
    public int publishContent(PublishContentDto publishContentDto) {
        Long itemId = snowflake.nextId();
        // 1.判断是否送审 如果送审异步发送到mq
        //根据创作者id来判断创作者的粉丝数量 如果粉丝数量大于某个动态配置的值那么就必须送审

        // 查询创作者的信息，如果创作者近期经常被举报那么也必须送审(值也是动态的)

        // 当内容的一些数量评论，点赞，观看量超过了某一个值，那么也必须送审(值也是动态的)

        // 当改内容近期被多次投诉，那么也要送审

        //其余情况交给人工智能审核
        // 持久化
        UtcItemInfo utcItemInfo = new UtcItemInfo();
        UtcItemRecord utcItemRecord = new UtcItemRecord();
        utcItemInfo.setItemId(itemId);
        utcItemInfo.setCreatorId(publishContentDto.getCreatorId());
        utcItemInfo.setLastVersion(defaultVersion);
        utcItemInfo.setOnlineTextUri(publishContentDto.getOnlineTextUri());
        utcItemInfo.setVisibility(publishContentDto.getVisibility());
        utcItemInfo.setCreateTime(publishContentDto.getCreateTime());
        //默认为为未审核
        utcItemInfo.setStatus(AuditEnum.PENDING_AUDIT.getValue());

        utcItemRecord.setItemId(itemId);
        utcItemRecord.setAuditStatus(AuditEnum.PENDING_AUDIT.getValue());
        utcItemRecord.setLastestTextUri(publishContentDto.getOnlineTextUri());
        utcItemRecord.setLastestStatus(AuditEnum.PENDING_AUDIT.getValue());
        utcItemRecord.setUpdateTime(publishContentDto.getCreateTime()); // 第一次的修改时间其实就是创建的时间
        utcItemRecord.setLastestVersion(defaultVersion);


        int i = utcItemInfoMapper.insertUtcItemInfo(utcItemInfo);
        int i1 = utcItemRecordMapper.insertUtcItemRecord(utcItemRecord);
        return i + i1;
    }

    @Override
    @Transactional
    public int updateContent(UtcItemInfo utcItemInfo) {
        int i = utcItemInfoMapper.updateUtcItemInfo(utcItemInfo);
        UtcItemRecord utcItemRecord = new UtcItemRecord();
        utcItemRecord.setItemId(utcItemInfo.getItemId());
        utcItemRecord.setAuditStatus(AuditEnum.PENDING_AUDIT.getValue());
        utcItemRecord.setLastestTextUri(utcItemInfo.getOnlineTextUri());
        utcItemRecord.setLastestStatus(AuditEnum.PENDING_AUDIT.getValue());
        utcItemRecord.setUpdateTime(utcItemInfo.getCreateTime()); // 第一次的修改时间其实就是创建的时间
        utcItemRecord.setLastestVersion(defaultVersion + 1);
        int i1 = utcItemRecordMapper.insertUtcItemRecord(utcItemRecord);
        return i + i1;
    }
}
